Introduction

photo_data %>%
  select(pageURL) %>%
  knitr::kable()
pageURL
https://pixabay.com/photos/ocean-milky-way-boat-sailing-3605547/
https://pixabay.com/photos/milky-way-stars-night-sky-2695569/
https://pixabay.com/photos/aurora-northern-lights-1197753/
https://pixabay.com/photos/astronomy-bright-constellation-dark-1867616/
https://pixabay.com/photos/milky-way-starry-sky-night-sky-8258264/
https://pixabay.com/photos/night-view-starry-sky-milky-way-8306605/
https://pixabay.com/photos/starry-sky-milky-way-mountains-camp-8199764/
https://pixabay.com/photos/stars-sky-night-starry-sky-1837306/
https://pixabay.com/photos/lighthouse-starry-sky-universe-6785763/
https://pixabay.com/photos/starry-sky-night-sky-stars-night-2675322/
https://pixabay.com/photos/starry-sky-milky-way-night-sky-2051448/
https://pixabay.com/photos/starry-sky-universe-milky-way-grove-8041247/
https://pixabay.com/photos/mountains-starry-sky-milky-way-8031511/
https://pixabay.com/photos/night-sky-stars-snow-trees-night-7733876/
https://pixabay.com/photos/stars-sky-night-starry-sky-1845140/
https://pixabay.com/photos/stars-sky-night-starry-night-sky-1654074/
https://pixabay.com/photos/milky-way-human-starry-sky-star-4667644/
https://pixabay.com/photos/stars-milky-way-night-sky-blue-sky-1869692/
https://pixabay.com/photos/stars-sky-milky-way-galaxy-1869447/
https://pixabay.com/photos/starry-sky-stars-galaxies-1655503/
https://pixabay.com/photos/dark-sky-stars-night-outdoors-1850638/
https://pixabay.com/photos/stars-night-sky-space-galaxy-1655504/
https://pixabay.com/photos/milky-way-starry-sky-night-sky-star-5295160/
https://pixabay.com/photos/star-trails-night-sky-field-stars-5718301/
https://pixabay.com/photos/night-sky-tree-field-stars-starry-6245049/
https://pixabay.com/photos/stars-sky-night-starry-night-1845852/
https://pixabay.com/photos/milky-way-river-starry-sky-6496937/
https://pixabay.com/photos/backlit-stars-night-sky-beautiful-1845281/
https://pixabay.com/photos/sky-night-galaxy-milky-way-stars-1281662/
https://pixabay.com/photos/tent-camping-hills-man-silhouette-5441144/
https://pixabay.com/photos/starry-sky-flower-meadow-night-5410078/
https://pixabay.com/photos/night-sky-mountains-stars-galaxy-5742416/
https://pixabay.com/photos/night-sky-lake-rocks-stones-stars-6039591/
https://pixabay.com/photos/sky-milky-way-cosmos-night-dark-4576072/
https://pixabay.com/photos/buildings-skyscrapers-train-5965278/
https://pixabay.com/photos/milky-way-starry-sky-stars-night-4448583/
https://pixabay.com/photos/people-lovers-night-sky-starry-sky-1215160/
https://pixabay.com/photos/starry-sky-heaven-mountain-2099828/
https://pixabay.com/photos/trees-starry-sky-rock-moss-4329040/
https://pixabay.com/photos/phase-of-the-moon-night-star-4969110/
https://pixabay.com/photos/lake-traveler-night-sky-water-5637585/
https://pixabay.com/photos/milky-way-starry-sky-night-sky-star-5295155/
https://pixabay.com/photos/galaxy-starry-sky-stars-ngc-4414-11139/
https://pixabay.com/photos/starry-sky-stars-mountains-1946936/
https://pixabay.com/photos/airplane-starry-sky-milky-way-7873308/
https://pixabay.com/photos/milky-way-starry-sky-sky-star-2078342/
https://pixabay.com/photos/milky-way-astro-starry-sky-stars-2181250/
https://pixabay.com/photos/desert-night-starry-sky-person-2897107/
https://pixabay.com/photos/stars-night-sky-pyramids-sphinx-1096934/
https://pixabay.com/photos/moon-eclipse-starry-sky-hills-5338374/
https://pixabay.com/photos/galaxy-starry-sky-tibet-tibet-2377474/
https://pixabay.com/photos/milky-way-star-starry-sky-cosmos-2740777/
https://pixabay.com/photos/stars-starry-sky-night-sky-7531711/
https://pixabay.com/photos/starry-sky-night-shot-night-sky-2116792/
https://pixabay.com/photos/night-shot-starry-sky-night-sky-2180474/
https://pixabay.com/photos/moose-animal-starry-sky-nature-1793632/
https://pixabay.com/photos/milky-way-night-sky-stars-559641/
https://pixabay.com/photos/sea-rowboat-starry-sky-boat-ocean-7214616/
https://pixabay.com/photos/starry-sky-stars-night-sky-astro-1775745/
https://pixabay.com/photos/starry-sky-night-hills-the-bonfire-1656491/
https://pixabay.com/photos/night-shot-starry-sky-night-sky-2553103/
https://pixabay.com/photos/night-sky-trees-forest-blue-4072727/
https://pixabay.com/photos/stars-starry-sky-night-galaxy-8321032/
https://pixabay.com/photos/milky-way-night-sky-sea-ocean-5727732/
https://pixabay.com/photos/natural-starry-sky-night-view-2065714/
https://pixabay.com/photos/starry-sky-galaxy-fog-background-5039634/
https://pixabay.com/photos/milky-way-starry-sky-night-bridge-8033188/

Key features of selected photos

#Calculate the amount of photos which is more million downloads among the selected photos.
amount_more_million_downloads <- photo_data %>%
  group_by(more_million_downloads) %>%
  summarise(amount = n())
amount_more_million_downloads
## # A tibble: 2 × 2
##   more_million_downloads amount
##   <chr>                   <int>
## 1 no                         63
## 2 yes                         4

Creativity

Create a plot of the ratio of downloads to views for each user by using group_by() and summarise() function and using the summary value into ggplot() and geom_point() function.

#Calculate the ratio of between downloads and views for each users 
user_download_ratio <- photo_data %>%
  group_by(user_id) %>%
  summarise(downloads_ratio = sum(downloads)/sum(views))

#Create a plot of the ratio of downloads to views for each user
user_download_ratio_plot <- user_download_ratio %>%
  ggplot() +
  geom_point(aes(x=user_id, y = downloads_ratio)) +
  labs(
    title = "The downloads ratio of users",
    subtitle = "The ratio of downloads to views for each user",
    caption = "Source: Pixabay",
    x = "User ID",
    y = "Downloads Ratio"
  )
options(scipen = 999)
user_download_ratio_plot

Learning reflection

From the Module 3:Creating new variables and data tables, I learned to identify key features of JSON, how to write simple queries using SQL, how to manipulate data frames using {dplyr}, how to source data from an API, how to report on key features of data, how to use {dplyr} to produce summaries, how to using R & RMarkdown for reproducible reporting including drop_na(), na.omit(), inline R code & knitr::kable(). And there are some important functions I learned in module 3, such as fromJSON(),mutate(),select(),group_by(),summarise(),geom_point().

In the future, I want to learn more about how to analyse the data using some packages on the R.

Appendix

library(magick)
library(tidyverse)
library(jsonlite)
#Explore the code!
json_data <- fromJSON("pixabay_data.json")
pixabay_photo_data <- json_data$hits

selected_photos <- pixabay_photo_data %>%
  select(previewURL,pageURL,downloads,tags, likes, user_id, views, comments) %>%
  mutate(tags_length = str_count(tags, "\\S+")) %>%
  mutate(more_million_downloads = ifelse(downloads > 1000000, "yes", "no")) %>%
  mutate(if_contain_search_words = ifelse(str_detect(str_to_lower(tags),
                                                     "starry|sky"),
                                          "yes",
                                          "no")) %>%
  filter(more_million_downloads == "yes" | if_contain_search_words == "yes" ) 

write_csv(selected_photos, "selected_photos.csv")

#Calculate the mean downloads of selected photos
mean_downloads <- selected_photos$downloads %>%
  mean()
mean_downloads

#Calculate the mean tags_length of selected photos
mean_tags_length <- selected_photos$tags_length %>%
  mean()
mean_tags_length

#Calculate the sum of comments of selected photos
sum_comments <- selected_photos$comments %>%
  sum()
sum_comments

#Calculate the sum of more million downloads and less million downloads
sum_more_million_downloads <- selected_photos %>%
  group_by(more_million_downloads) %>%
  summarise(sum_downloads = sum(downloads))
sum_more_million_downloads

# Calculate the mean likes of different users
user_likes <- selected_photos %>%
  group_by(user_id) %>%
  summarise(mean_like = mean(likes))
user_likes

#Calculate the amount of photos which is more million downloads among the selected photos.
amount_more_million_downloads <- photo_data %>%
  group_by(more_million_downloads) %>%
  summarise(amount = n())
amount_more_million_downloads

#Calculate the ratio of between downloads and views for each users 
user_download_ratio <- selected_photos %>%
  group_by(user_id) %>%
  summarise(downloads_ratio = sum(downloads)/sum(views))
user_download_ratio

#Create an animated GIF of the photos in your selected_photos data frame
img_urls <- selected_photos$previewURL %>% na.omit()

img_urls %>%
  image_read() %>%
  image_scale(200) %>%
  image_animate(fps = 2) %>%
  image_write("my_photos.gif")

#Create a plot of the ratio of downloads to views for each user
user_download_ratio_plot <- user_download_ratio %>%
  ggplot() +
  geom_point(aes(x=user_id, y = downloads_ratio)) +
  labs(
    title = "The downloads ratio of users",
    subtitle = "The ratio of downloads to views for each user",
    caption = "Source: Pixabay",
    x = "User ID",
    y = "Downloads Ratio"
  )
options(scipen = 999)
user_download_ratio_plot